package com.example.qinghe.frontController.sale;


import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.entity.sale.Connectionqh;
import com.example.qinghe.entity.sale.Opportunityqh;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.service.sale.ConnectionqhService;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/front/connectionqh")
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
@Api(tags = "销售管理->联系记录", description = "FrontConnectionqhController")
public class FrontConnectionqhController {

    @Autowired
    private ConnectionqhService connectionqhService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("传客户id得到这个客户的所有联系记录")
    @GetMapping(value = "/selectConnectionqhByCustomerId/{id}")
    public Result selectConnectionqhByCustomerId(@PathVariable("id") Long id){
        List<Connectionqh> connectionqhs = connectionqhService.selectConnectionqhByCustomerId(id);
        return connectionqhs==null? Result.error("无数据"):Result.success(connectionqhs);
    }

    // 传商机id获得商机的所有联系记录
    @ApiOperation("传商机id获得这个商机的所有联系记录")
    @GetMapping(value = "/selectConnectionqhByOpportunityId/{id}")
    public Result selectConnectionqhByOpportunityId(@PathVariable("id") Long id){
        List<Connectionqh> connectionqhs = connectionqhService.selectConnectionqhByOpportunityId(id);
        return connectionqhs==null? Result.error("无数据"):Result.success(connectionqhs);
    }

    // 查找所有相关联系人
    @ApiOperation("查找客户所有可能相关的联系人")
    @GetMapping(value = "/selectAllRelativeStaff")
    public Result selectAllRelativeStaff(HttpServletRequest request){
        Staff staff = userService.getStaff(request);
        if (staff == null)
            return Result.error("请先登录！");
        List<Staff> staff1 = connectionqhService.selectAllRelativeStaff(staff.getId());
        return staff1==null? Result.error("无数据"):Result.success(staff1);
    }

    // 查找所有相关商机
    @ApiOperation("查找所有商机/传当前商机id 如果是新增客户联系就传0")
    @GetMapping(value = "/selectAllRelativeOpportunity/{id}")
    public Result selectAllRelativeOpportunity(@PathVariable("id") Long id){
        List<Opportunityqh> opportunityqhs = connectionqhService.selectAllRelativeOpportunity(id);
        return opportunityqhs==null? Result.error("无数据"):Result.success(opportunityqhs);
    }







    @ApiOperation("添加当前员工与客户/商机的联系记录")
    @PostMapping(value = "/insertConnectionqh")
    public Result insertConnectionqh(@ApiParam(name = "customerid",value = "客户id") @RequestParam Long customerid,@ApiParam(name = "opportunityid",value = "商机id") @RequestParam Long opportunityid,
                                     @ApiParam(name = "relcontactid",value = "相关联系人id") @RequestParam Long relcontactid,@ApiParam(name = "relopportid",value = "相关商机id") @RequestParam Long relopportid,
                                     @ApiParam(name = "content",value = "联系内容") @RequestParam("content") String content,HttpServletRequest request ){
        Staff staff = userService.getStaff(request);
        if (staff == null)
            return Result.error("请先登录！");

        Connectionqh connectionqh=new Connectionqh();
        connectionqh.setStaffid(staff.getId());
        connectionqh.setCustomerid(customerid);
        connectionqh.setOpportunityid(opportunityid);
        connectionqh.setRelcontactid(relcontactid);
        connectionqh.setRelopportid(relopportid);

        connectionqh.setContent(content);
        connectionqh.setContacttime(LocalDateTime.now());
        int insertConnectionqh = connectionqhService.insertConnectionqh(connectionqh);
        return insertConnectionqh==0?Result.error("添加失败"):Result.success("添加成功");
    }
}
